package com.thiny.android.game.thunderbolt.role;

import com.thiny.android.game.thunderbolt.util.Constants;
import com.thiny.android.game.thunderbolt.view.GameView;

/**
 * 敌机类
 * 
 * @author THINY
 * 
 */
public class Enemy extends Plane {

	private Player mPlayer;

	private float speedX, speedY;
	// 飞向目标时，第一次计算用
	private boolean isFirstCalculate = true;
	// 是否开火
	private boolean isFire = true;

	public Enemy(GameView gameView, float x, float y, int type, int speed,
			int life, Player player) {
		super(gameView, x, y, Constants.DIR_DOWN, type, speed, life);
		mPlayer = player;
		if (this.x < BITMAP_WIDTH / 2) {
			this.x = BITMAP_WIDTH / 2;
		}
		if (this.x > SCREEN_WIDTH - BITMAP_WIDTH / 2) {
			this.x = SCREEN_WIDTH - BITMAP_WIDTH / 2;
		}
	}

	@Override
	public void fire() {

		if (!isFire) {
			return;
		}
		int bulletType = 0;
		int bulletSpeed = Constants.ENEMY_BULLET_SPEED;
		switch (type) {
		case Constants.ENEMY_TYPE_1:
			bulletType = Constants.ENEMY_BULLET_1;
			break;
		case Constants.ENEMY_TYPE_2:
			bulletType = Constants.ENEMY_BULLET_1;
			break;
		case Constants.ENEMY_TYPE_3:
			bulletType = Constants.ENEMY_BULLET_3;
			bulletSpeed = Constants.ENEMY_BULLET_SPEED_FAST;
			break;
		case Constants.ENEMY_TYPE_4:
			bulletType = Constants.ENEMY_BULLET_2;
			bulletSpeed = Constants.ENEMY_BULLET_SPEED_FAST;
			break;
		case Constants.ENEMY_TYPE_BOSS1:
			bulletType = Constants.ENEMY_BULLET_1;
			bulletSpeed = Constants.ENEMY_BULLET_SPEED_FAST;
			break;
		case Constants.ENEMY_TYPE_BOSS2:
			bulletType = Constants.ENEMY_BULLET_2;
			bulletSpeed = Constants.ENEMY_BULLET_SPEED_FAST;
			break;
		case Constants.ENEMY_TYPE_BOSS3:
			bulletType = Constants.ENEMY_BULLET_2;
			bulletSpeed = Constants.ENEMY_BULLET_SPEED_FAST;
			break;
		}

		switch (type) {
		case Constants.ENEMY_TYPE_BOSS1:
			bossBullet1(bulletType, bulletSpeed);
			break;
		case Constants.ENEMY_TYPE_BOSS2:
			bossBullet2(bulletType, bulletSpeed);
			break;
		case Constants.ENEMY_TYPE_BOSS3:
			bossBullet3(bulletType, bulletSpeed);
			break;
		default:
			mGameView.addEnemyBullet(new Bullet(mGameView, x, y + BITMAP_HEIGHT
					/ 2, Constants.DIR_DOWN, bulletType, bulletSpeed));
			break;
		}
	}

	// 第一关的boss子弹
	private void bossBullet1(int bulletType, int bulletSpeed) {
		mGameView.addEnemyBullet(new Bullet(mGameView, x,
				y + BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				- mGameView.getBitmapByBulletType(bulletType).getWidth() * 2, y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				+ mGameView.getBitmapByBulletType(bulletType).getWidth() * 2, y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
	}

	// 第二关的boss子弹
	private void bossBullet2(int bulletType, int bulletSpeed) {
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				- mGameView.getBitmapByBulletType(bulletType).getWidth(), y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				+ mGameView.getBitmapByBulletType(bulletType).getWidth(), y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				- mGameView.getBitmapByBulletType(bulletType).getWidth() * 3, y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
		mGameView.addEnemyBullet(new Bullet(mGameView, x
				+ mGameView.getBitmapByBulletType(bulletType).getWidth() * 3, y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));
	}

	/**
	 * 第三关的boss子弹,水平铺满子弹（间距2个飞机空间） 子弹位置确定方法如下： 先于正中央设置一颗子弹
	 * 然后以中心子弹展开，向两边扩展，每隔2个飞机宽度距离添加一颗子弹
	 * 
	 * @param bulletType
	 * @param bulletSpeed
	 */
	private void bossBullet3(int bulletType, int bulletSpeed) {

		int playerWidth = (int) mPlayer.BITMAP_WIDTH;
		int bulletWidth = mGameView.getBitmapByBulletType(bulletType)
				.getWidth();
		int distance = bulletWidth + playerWidth * 2;
		// 计算半个屏幕，一次性能容纳多少发boss子弹
		int num = (int) ((SCREEN_WIDTH - bulletWidth) / (bulletWidth * 2 + playerWidth * 4));
		// 往正中央添加
		mGameView.addEnemyBullet(new Bullet(mGameView, SCREEN_WIDTH / 2, y
				+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
				bulletSpeed));

		for (int i = 1; i <= num; i++) {
			mGameView.addEnemyBullet(new Bullet(mGameView, SCREEN_WIDTH / 2
					- distance*i, y
					+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
					bulletSpeed));
			mGameView.addEnemyBullet(new Bullet(mGameView, SCREEN_WIDTH / 2
					+distance*i, y
					+ BITMAP_HEIGHT / 2, Constants.DIR_DOWN, bulletType,
					bulletSpeed));
		}
	}

	/**
	 * 
	 * 敌机移动
	 * 
	 */
	@Override
	public void move() {

		/**
		 * 
		 * 先判断是否为BOSS ,再处理其它
		 * 
		 * y坐标在60前，所有飞机(不包括boss)都是直线下降，后的再根据类型判断处理
		 */

		if (type == Constants.ENEMY_TYPE_BOSS1
				|| type == Constants.ENEMY_TYPE_BOSS2
				|| type == Constants.ENEMY_TYPE_BOSS3) {

			if (y < BITMAP_HEIGHT * 2 / 3) {
				y += speed;
			} else {

				if (x < BITMAP_WIDTH * 2 / 3) {
					speed = -speed;
				} else if (x > SCREEN_WIDTH - BITMAP_WIDTH * 2 / 3) {
					speed = -speed;
				}
				x += speed;
			}

		} else if (type == Constants.ENEMY_TYPE_2) {
			if (x < mPlayer.getX()) {
				x += Constants.ENEMY_SPEED_OFFSET;
				y += speed;
			} else {
				x -= Constants.ENEMY_SPEED_OFFSET;
				y += speed;
			}
			if (Math.abs(x - mPlayer.getX()) < 2) {
				x = mPlayer.getX();
			}
		} else if ((isFire && y < 60) || type == Constants.ENEMY_TYPE_1
				|| type == Constants.ENEMY_TYPE_3) {
			y += speed;
		} else {
			if (isFirstCalculate) {
				calculateSpeed();
			}
			x -= speedX;
			y -= speedY;
		}

	}

	// 停止开火，向目标加速前进
	private void calculateSpeed() {
		// 敌机轰炸的目标(玩家)的坐标值
		float targetX = mPlayer.getX();
		float targetY = mPlayer.getY();

		double distance = Math.sqrt((x - targetX) * (x - targetX)
				+ (y - targetY) * (y - targetY));

		speedX = (float) ((x - targetX) * Constants.ENEMY_SPEED_FAST / distance);
		speedY = (float) ((y - targetY) * Constants.ENEMY_SPEED_FAST / distance);
		isFirstCalculate = false;
		isFire = false;
	}

	/**
	 * 根据类型，设置bitmap
	 * 
	 */
	@Override
	public void setRoleBitmap(int type) {

		switch (type) {
		case Constants.ENEMY_TYPE_1:
			mRoleBitmap = mGameView.enemy1;
			break;
		case Constants.ENEMY_TYPE_2:
			mRoleBitmap = mGameView.enemy2;
			break;
		case Constants.ENEMY_TYPE_3:
			mRoleBitmap = mGameView.enemy3;
			break;
		case Constants.ENEMY_TYPE_4:
			mRoleBitmap = mGameView.enemy4;
			break;
		case Constants.ENEMY_TYPE_BOSS1:
			mRoleBitmap = mGameView.enemy_boss1;
			break;
		case Constants.ENEMY_TYPE_BOSS2:
			mRoleBitmap = mGameView.enemy_boss2;
			break;
		case Constants.ENEMY_TYPE_BOSS3:
			mRoleBitmap = mGameView.enemy_boss3;
			break;
		}

	}

	// 判断敌机是否为boss
	public boolean isBoss() {
		return type >= Constants.ENEMY_TYPE_BOSS1 ? true : false;
	}

}
